<?php
function iserializer($value) {
	return serialize($value);
}


function iunserializer($value) {
	if (empty($value)) {
		return array();
	}
	if (!is_serialized($value)) {
		return $value;
	}
	if (version_compare(PHP_VERSION, '7.0.0', '>=')) {
		$result = unserialize($value, array('allowed_classes' => false));
	} else {
		if (preg_match('/[oc]:[^:]*\d+:/i', $value)) {
			return array();
		}
		$result = unserialize($value);
	}
	if (false === $result) {
		$temp = preg_replace_callback('!s:(\d+):"(.*?)";!s', function ($matchs) {
			return 's:' . strlen($matchs[2]) . ':"' . $matchs[2] . '";';
		}, $value);

		return unserialize($temp);
	} else {
		return $result;
	}
}

function is_serialized($data, $strict = true) {
	if (!is_string($data)) {
		return false;
	}
	$data = trim($data);
	if ('N;' == $data) {
		return true;
	}
	if (strlen($data) < 4) {
		return false;
	}
	if (':' !== $data[1]) {
		return false;
	}
	if ($strict) {
		$lastc = substr($data, -1);
		if (';' !== $lastc && '}' !== $lastc) {
			return false;
		}
	} else {
		$semicolon = strpos($data, ';');
		$brace = strpos($data, '}');
				if (false === $semicolon && false === $brace) {
			return false;
		}
				if (false !== $semicolon && $semicolon < 3) {
			return false;
		}
		if (false !== $brace && $brace < 4) {
			return false;
		}
	}
	$token = $data[0];
	switch ($token) {
		case 's':
			if ($strict) {
				if ('"' !== substr($data, -2, 1)) {
					return false;
				}
			} elseif (false === strpos($data, '"')) {
				return false;
			}
						case 'a':
			return (bool) preg_match("/^{$token}:[0-9]+:/s", $data);
		case 'O':
			return false;
		case 'b':
		case 'i':
		case 'd':
			$end = $strict ? '$' : '';

			return (bool) preg_match("/^{$token}:[0-9.E-]+;$end/", $data);
	}

	return false;
}

function random($length, $numeric = false) {
	$seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
	$seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
	if ($numeric) {
		$hash = '';
	} else {
		$hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
		--$length;
	}
	$max = strlen($seed) - 1;
	for ($i = 0; $i < $length; ++$i) {
		$hash .= $seed[mt_rand(0, $max)];
	}

	return $hash;
}

function get_image_full_url($thumb, $hosts = false) {
	
	if (empty($thumb)) {
		return 'https://www.hzgsp.com/static/theme/img/404_icon.png';
	}

	if ( strpos($thumb, 'http') !== false ) {
		return $thumb;
	}

	if ( strpos($thumb, 'https') !== false ) {
		return $thumb;
	}
	
	if ($hosts) {
		return $hosts . $thumb;
	}else{
		return 'https://oos.hzgsp.com/' . $thumb;
	}
}

//获取请求IP
if (!function_exists('get_access_ip')) {
	function get_access_ip() {
	    //strcasecmp 比较两个字符，不区分大小写。返回0，>0，<0。
	    if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
	        $ip = getenv('HTTP_CLIENT_IP');
	    } else if (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
	        $ip = getenv('HTTP_X_FORWARDED_FOR');
	    } else if (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
	        $ip = getenv('REMOTE_ADDR');
	    } else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
	        $ip = $_SERVER['REMOTE_ADDR'];
	    }
	    $res =  preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
	    return $res;
	}
}

//获取两个经纬度之间距离
if (!function_exists('haversineGreatCircleDistance')) {
	function haversineGreatCircleDistance( $latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)  
	{
	    // 将角度转换为弧度  
	    $latFrom = deg2rad($latitudeFrom);
	    $lonFrom = deg2rad($longitudeFrom);
	    $latTo = deg2rad($latitudeTo);
	    $lonTo = deg2rad($longitudeTo);

	    $latDelta = $latTo - $latFrom;
	    $lonDelta = $lonTo - $lonFrom;

	    $angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) + cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));  
	    return $angle * $earthRadius;  
	}
}

/**
     * 过滤微信昵称，保留Emoji
     */
if (!function_exists('filterWechatNickname')) {
    function filterWechatNickname($nickname) {
        // 去除HTML标签
        $clean = strip_tags($nickname);
        
        // 去除不可见字符
        $clean = preg_replace('/[\x00-\x1F\x7F]/u', '', $clean);
        
        // 替代方案 - 使用显式字符范围
        $clean = preg_replace('/[^a-zA-Z0-9\s\x{4e00}-\x{9fa5}\x{1F600}-\x{1F64F}\p{P}]/u', '', $clean);
        
        // 去除首尾空格
        $clean = trim($clean);
        
        // 限制长度（微信昵称最长32字符）
        return mb_substr($clean, 0, 32, 'UTF-8');
    }
}

?>